package cn.big.dubbo.demo.web;

import cn.big.dubbo.demo.service.IDemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

@Controller
public class DemoController {

	private Logger logger = LoggerFactory.getLogger(DemoController.class);

	@Autowired
	private IDemoService demoService;

	@Autowired
	private ApplicationContext applicationContext;

	private Set<IDemoService> demoServiceSet = new CopyOnWriteArraySet<>();

	@RequestMapping("test")
	@ResponseBody
	public void test() {
		String demoServiceBeanName = "xxxx";
		//从这边获取
		Object xxxx = applicationContext.getBean(demoServiceBeanName);
		if (xxxx.equals(demoService)) {
			logger.info("IDemoService的BeanName：{}", demoServiceBeanName);
		}
		demoService.test();
		demoServiceSet.add(demoService);
		/**
		 * since 2019-01-23 服务提供者如果是集群的情况，客户端根据RegistryDirectory的urlInvokerMap（Invoker集合），
		 * 根据LoadBalance接口的实现，选择一个Invoker进行访问。
		 */
		logger.info("此时有{}个不同的demoService", demoServiceSet.size());
	}
}
